﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using Volo.Abp.OpenIddict.Applications;

namespace Himp.Platform
{
    public class AppModuleService : CrudAppService<AppModule, AppModuleDto, Guid, AppModuleInput, AppModuleCreateDto, AppModuleUpdateDto>
    {
        private IRepository<AppModule, Guid> _NamespaceConfigRepository;

        public AppModuleService(IRepository<AppModule, Guid> NamespaceConfigRepository) : base(NamespaceConfigRepository)
        {
            _NamespaceConfigRepository = NamespaceConfigRepository;
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<AppModuleDto>> GetListAsync(AppModuleInput input)
        {
            var iQuery = await _NamespaceConfigRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Filter), i => i.ModuleName.Contains(input.Filter));
            iQuery = iQuery.OrderBy(i => i.Srtno);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<AppModule>, List<AppModuleDto>>(iQuery.ToList());

                return new PagedResultDto<AppModuleDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<AppModule>, List<AppModuleDto>>(items.ToList());

                return new PagedResultDto<AppModuleDto>(totalCount, itemsDto);
            }
        }
    }
}
